package databook.util;

import java.util.HashMap;
import java.util.Arrays;
import java.util.Vector;
import java.util.Iterator;
import java.io.FileNotFoundException;
import prefuse.data.io.DataIOException;

import prefuse.data.Tuple;
import prefuse.data.Table;

/**
 *
 * @author zach
 */
public class MajorMap {
    private static String datafile = "acad_tbl.csv";
    
    private static final String MAJOR_ACNM = "LSA_ACAD_PROG_CD";
    private static final String MAJOR_NAME = "LSA_ACAD_PROG_LD";
    private static final String DEPT_NAME = "LSA_RESP_DEPT_LD";
    private static final String COLLEGE_ACNM = "LSA_COLLEGE_SD";
    private static final String COLLEGE_NAME = "LSA_COLLEGE_LD";
    
    private static final String UNKNOWN = "unk";
    
    private static HashMap<String,Tuple> map;
    private static HashMap<String,String> deptMap;
    private static HashMap<String,String> collegeMap;
    
    static {
        loadFrom(datafile);
    }
    
    public static void loadFrom(String file) {
        CSVReader csv = new CSVReader(file);
        Table table;
        map = new HashMap();
        deptMap = new HashMap();
        collegeMap = new HashMap();

        try {
            table = csv.parseTable();
            for (int i = 0; i < table.getTupleCount(); i++) {
                map.put(table.getString(i,MAJOR_ACNM),table.getTuple(i));
                deptMap.put(table.getString(i,DEPT_NAME),table.getString(i,COLLEGE_ACNM));
                collegeMap.put(table.getString(i,COLLEGE_ACNM),table.getString(i,COLLEGE_NAME));
            }
        } catch (FileNotFoundException fnfe) {
            System.err.println(fnfe);
        } catch (DataIOException dioe) {
            System.err.println(dioe);
        }
    }
    
    public static String getDepartment(String major) {
        return get(major,DEPT_NAME);
    }
    
    public static String getCollege(String major) {
        return get(major,COLLEGE_ACNM);
    }
    
    public static String getCollegeName(String college) {
        if (collegeMap.containsKey(college))
            return collegeMap.get(college);
        else
            return UNKNOWN;
    }
    
    public static String getCollegeFromDept(String dept) {
        if (deptMap.containsKey(dept))
            return deptMap.get(dept);
        else
            return UNKNOWN;
    }
    
    public static boolean isMajor(String major) {
        return map.containsKey(major);
    }
    
    public static boolean isDept(String dept) {
        return deptMap.containsKey(dept);
    }
    
    public static boolean isCollege(String college) {
        return collegeMap.containsKey(college);
    }
    
    public static String getMajorName(String major) {
        return get(major,MAJOR_NAME);
    }
    
    public static String[] getSortedDeptments() {
        return getSortedKeys(deptMap);
    }
    
    public static String[] getSortedMajor() {
        return getSortedKeys(map);
    }
    
    public static String[] getSortedKeys(HashMap hmap) {
        Object[] keys = hmap.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        return (String[])keys;
    }
    
    public static String[] getSortedMajors(String dept) {
        Vector<String> majors = new Vector();
        Iterator iter = map.values().iterator();
        while(iter.hasNext()) {
            Tuple t = (Tuple)iter.next();
            if (t.getString(DEPT_NAME).equals(dept)) {
                majors.add(t.getString(MAJOR_ACNM));
            }
        }
        String[] ret = majors.toArray(new String[0]);
        Arrays.sort(ret);
        return ret;
    }

    protected static String get(String key, String field) {
        if (map.containsKey(key)) 
            return map.get(key).getString(field);
        else
            return UNKNOWN;
    }

}
